<?php
/**
 * stats/versions.php [last modified: 2010-05-04]
 * 
 * project:	expCounter
 * version:	1.2
 * copyright:	© 2009 by Magnus Geisler
 * license:	GPL 2.0 or higher(see docs/license.txt)
 *
 * based on chCounter 3.1.3 by Christoph Bachner and Bert Körn
 */
error_reporting(E_ALL);





// Kompatibilität für verschiedenen PHP5 Versionen verbessern
if(version_compare(PHP_VERSION, '5.3.0', '<')) set_magic_quotes_runtime(0);
if(version_compare(PHP_VERSION, '5.1.0', '>=')) date_default_timezone_set('Europe/Berlin');

header( 'Content-Type: text/html; charset=utf-8' ); 
@session_start();
@ini_set( 'arg_separator.output', '&amp;' );


// register_globals off emulieren, falls nötig
require_once('../includes/protection.class.php');
PROTECTION::unregisterGLOBALS();


// chCounter root path
define( 'CHC_ROOT', dirname( dirname( __FILE__ ) ) );

//require_once( CHC_ROOT .'/includes/user_agents.lib.php' );
require_once( CHC_ROOT .'/includes/functions.inc.php' );
require_once( CHC_ROOT .'/includes/template.class.php' );
require_once( CHC_ROOT .'/includes/config.inc.php' );
require_once( CHC_ROOT .'/includes/common.inc.php' );
require_once( CHC_ROOT .'/includes/mysql.class.php' );

$_CHC_DB = new chC_mysql( $_CHC_DBCONFIG['server'], $_CHC_DBCONFIG['user'], $_CHC_DBCONFIG['password'], $_CHC_DBCONFIG['database'] );

$_CHC_TPL = new chC_template( CHC_ROOT .'/templates/stats/versions_keyw.tpl.html' );

$_CHC_CONFIG = chC_get_config();


// which language?
$available_languages = chC_get_available_languages( CHC_ROOT .'/languages' );
$lang = chC_get_language_to_use( $available_languages );
chC_send_select_list_to_tpl( 'COUNTER_LANGUAGES', $available_languages, $lang );
ob_start();
require_once( CHC_ROOT .'/languages/'. $lang .'/lang_config.inc.php' );
require_once( CHC_ROOT .'/languages/'. $lang .'/main.lang.php' );
ob_end_clean();


$chCounter_utf8_page_title = $_CHC_LANG['versions_keyw_variants'];
$chCounter_visible = 0;
$chC_seite_in_counterverzeichnis = 1;
if( $_CHC_CONFIG['counterstatus_statistikseiten'] == '0' )
{
	$chCounter_status = 'inactive';
}
$chCounter_force_new_db_connection = FALSE;
ob_start();
require_once( CHC_ROOT .'/counter.php' );
$counter_output = ob_get_contents();
ob_end_clean();


if( is_int( strpos( $_CHC_CONFIG['statistiken_login_erforderlich'], 'versions' ) ) )
{
	$login = chC_manage_login();
	if( chC_logged_in() == FALSE )
	{
		$failed_login = ( $login == -1 ) ? 1 : 0;
		print chC_get_login_page(
			$_CHC_LANG['versions_keyw_variants'],
			$failed_login,
			$counter_output
		);
		exit;
	}
}

// Timezone
$zeitzone = $_CHC_LANG['time_zones'][$_CHC_CONFIG['zeitzone']];
if( $_CHC_CONFIG['dst'] == '1' )
{
  $zeitzone .= ' '. $_CHC_LANG['time_zone_dst'];
}



$liste_monate = array(
	'01'	=> $_CHC_LANG['date']['January'],
	'02'	=> $_CHC_LANG['date']['February'],
	'03'	=> $_CHC_LANG['date']['March'],
	'04'	=> $_CHC_LANG['date']['April'],
	'05'	=> $_CHC_LANG['date']['May'],
	'06'	=> $_CHC_LANG['date']['June'],
	'07'	=> $_CHC_LANG['date']['July'],
	'08'	=> $_CHC_LANG['date']['August'],
	'09'	=> $_CHC_LANG['date']['September'],
	'10'	=> $_CHC_LANG['date']['October'],
	'11'	=> $_CHC_LANG['date']['November'],
	'12'	=> $_CHC_LANG['date']['December']
);

// Sortierung
if(isset($_GET['sortorder'])) {
  $sortorder = htmlentities($_GET['sortorder']) ;
} else {
  $sortorder = 'ASC' ;
}

//Sortierung umdrehen
If($sortorder == 'ASC') {
  $sortorder = 'DESC' ;
  $sortorder_toggled = 'ASC' ;
} else {
  $sortorder = 'ASC' ;
  $sortorder_toggled = 'DESC' ;
}


//Sortierfeld
if(!isset($_GET['sortfield'])){
  $sortfield = 'anzahl' ;
} else {
  $sortfield =  htmlentities($_GET['sortfield']) ;
}



if(!isset($_GET['typeinfo'])) {$_GET['typeinfo']='';}

if ($_GET['typeinfo'] == 'variants'){
// Suchwort Varianten, also im ganzen Wort enthalten
  $searchtype = 'suchwort' ;
  $searchcriteria = " AND wert LIKE '%".$_CHC_DB->real_escape_string( $_GET['type']) ."%' " ;
  
} else {
// Suchwort Kombinationen, also mehrere Worte
  $searchtype = 'suchphrase' ;
  $searchcriteria = " AND 
			(wert LIKE '% ".$_CHC_DB->real_escape_string( $_GET['type'] )."'
			OR 
			wert LIKE '".$_CHC_DB->real_escape_string( $_GET['type'] )." %'
			OR 
			wert LIKE '% ".$_CHC_DB->real_escape_string( $_GET['type'] )." %'
			OR 
			wert = '".$_CHC_DB->real_escape_string( $_GET['type'] )."') ";
}


$result = $_CHC_DB->query(
	"SELECT
		DISTINCT monat
	FROM `".CHC_TABLE_SEARCH_ENGINES."`
	WHERE
		typ LIKE '" . $searchtype . "'
		AND monat > 0
	ORDER BY monat DESC"
);

$liste = array(
	'all_months' => $_CHC_LANG['since_statistics_start'],
	'' => '--------------'
);

while( $row = $_CHC_DB->fetch_array( $result ) )
{
	$tmp_jahr = substr( $row['monat'], 0, 4 );
	$tmp_monat = substr( $row['monat'], 4 );
	$tmp_monat = str_replace( array_keys( $liste_monate ), $liste_monate, $tmp_monat );
	$liste[$row['monat']] = $tmp_monat .' '. $tmp_jahr;
}

$monat = ( isset( $_GET['month'] ) && !empty( $_GET['month'] ) && isset( $liste[$_GET['month']] ) ) ?  $_GET['month'] : chC_format_date( 'Ym', $_CHC_VARIABLES['timestamp_tagesanfang'], FALSE );
chC_send_select_list_to_tpl( 'MONTH', $liste, $monat );


if( $monat == 'all_months' )
{
	$result = $_CHC_DB->query(
		"SELECT wert, typ, SUM(anzahl) as anzahl
		FROM `".CHC_TABLE_SEARCH_ENGINES."`
		WHERE
			typ = '" . $searchtype . "'
			" . 
      $searchcriteria
      ."		GROUP BY wert
		ORDER by  " . $_CHC_DB->escape_string($sortfield) . " ". $_CHC_DB->escape_string($sortorder)
	);

	$gesamt = $_CHC_DB->query(
		"SELECT SUM( anzahl ) AS anzahl
		FROM `". CHC_TABLE_SEARCH_ENGINES ."`
		WHERE
			typ = '" . $searchtype . "'
			" . 
      $searchcriteria
      ."		LIMIT 0, 1"
	);
}	

else

{
	$result = $_CHC_DB->query(
		"SELECT wert, anzahl
		FROM `".CHC_TABLE_SEARCH_ENGINES."`
		WHERE
			typ = '" . $searchtype . "'
			" . 
      $searchcriteria
      ."
			
			AND monat = '". $monat ."'
		ORDER BY " . $sortfield . " ". $sortorder
	);

	$gesamt = $_CHC_DB->query(
		"SELECT SUM( anzahl ) AS anzahl
		FROM `". CHC_TABLE_SEARCH_ENGINES ."`
		WHERE
			typ = '" . $searchtype . "'
			" . 
      $searchcriteria
      ."
			AND monat = '". $monat ."'"
	);
}
$gesamt = $_CHC_DB->fetch_assoc( $gesamt );

$i = 0;

while( $row = $_CHC_DB->fetch_assoc( $result ) )
{
	$version = explode( '~', $row['wert'] );
	$version = $version[count( $version ) - 1];
	if( $_GET['type'] == 'others' )
	{
		$version = '[...]'. $version .'[...]';
	}
	$percent = @round( $row['anzahl'] / $gesamt['anzahl']* 100, 2 );
	$_CHC_TPL->add_block( 'KEYWORDSLIST', array(
			'ROW_CLASS_NR'	=> !( $i % 2 ) ? 1 : 2,
			'No.'		=> $i + 1,
			'VERSION'	=> $version == 'unknown' ? $_CHC_LANG['unknown'] : htmlentities($version, ENT_QUOTES, "UTF-8"),
			'VERSION_URL'	=> urlencode($version),
			'QUANTITY'	=> number_format( $row['anzahl'], 0, $_CHC_LANG['CONFIG']['decimal_separator'], $_CHC_LANG['CONFIG']['thousands_separator'] ),
			'PERCENTAGE' => number_format( $percent , 2, $_CHC_LANG['CONFIG']['decimal_separator'], $_CHC_LANG['CONFIG']['thousands_separator'] ),
			'GRAPH_PERCENTAGE' => (int) $percent,
		)
	);
	$i++;
}


if($_GET['typeinfo'] == 'variants'){
  //Keywordvarianten
  $_CHC_TPL->add_block( 'KEYWORDSVARIANTS', array(
  		'DUMMY' => 'Dummy'
  	)
  );
  $_CHC_TPL->assign( array(
    'L_VERSIONS_INFO'	=> $_CHC_LANG['versions_keyw_variants'],
    'L_KEYW_VARIANTS'	=> $_CHC_LANG['versions_keyw_variants'],
    'L_KEYW_KOMBIS'	=> $_CHC_LANG['versions_keyw_combinations']  
  ));
  
}	else {
  //KeywordKombinationen
  $_CHC_TPL->assign( array(
    'L_VERSIONS_INFO'	=> $_CHC_LANG['versions_keyw_combinations'],
    'L_KEYW_VARIANTS'	=> $_CHC_LANG['versions_keyw_variants'],
    'L_KEYW_KOMBIS'	=> $_CHC_LANG['versions_keyw_combinations']
  ));
}

$_CHC_TPL->assign( array(
    'YEARMONTH' => $_GET['month'],
    'VARIANTS' => $_GET['typeinfo'],
    'SORTFIELD' => $sortfield,
    'SORTORDER' => $sortorder,
    'SORTORDER_TOGGLED' => $sortorder_toggled,
    'SORTFIELD' => $sortfield,
		'V_TOTAL_VERSIONS' => $_CHC_DB->num_rows( $result ),
		'V_TOTAL_QUANTITY' => number_format( $gesamt['anzahl'], 0, $_CHC_LANG['CONFIG']['decimal_separator'], $_CHC_LANG['CONFIG']['thousands_separator'] ),
		'V_SCRIPT_VERSION' => $_CHC_CONFIG['script_version'],
		'L_GNU_GPL'	=> $_CHC_LANG['released_under_the_GNU_GPL'],
		'V_TRANSLATION_INFO'	=> !empty( $_CHC_LANG['CONFIG']['translator'] )
			? sprintf( $_CHC_LANG['translation_by:'], $available_languages[$lang], $_CHC_LANG['CONFIG']['translator'] )
			: '',

		'COUNTER' => $counter_output,
		'KEYWORD' => $_CHC_DB->real_escape_string($_GET['type']),

		'L_CLOSE_WINDOW' => $_CHC_LANG['close_window'], 
		'L_TOTAL:'	=> $_CHC_LANG['total:'],
		'L_OK'	=> $_CHC_LANG['OK'],
		'L_VERSION' => $_CHC_LANG['version'],
		'L_No.'	=> $_CHC_LANG['No.'],
		'L_QUANTITY'	=> $_CHC_LANG['quantity'],
		'L_PERCENTAGE'	=> $_CHC_LANG['percentage'],
		'L_LANGUAGE'	=> $_CHC_LANG['Language'],
		'L_NO_ENTRY_IN_DATABASE'	=> $_CHC_LANG['no_entry_in_database'],
		'L_SHOW' => $_CHC_LANG['show:'],
		'L_TIME_FRAME' => $_CHC_LANG['time_frame:'],
		'L_SEARCH_KEYWORD' => $_CHC_LANG['search_keyword'],
		
		// Footer
    'V_TIME_ZONE'	        => sprintf( $_CHC_LANG['time_zone'], $zeitzone ),
    'L_GO_TO_TOP'         => $_CHC_LANG['go_to_top']
	)
);

$_CHC_TPL->load_file( CHC_ROOT .'/templates/stats/index_footer.tpl.html' );
$_CHC_TPL->print_template();
?>
